//
//  BBetterView.swift
//  BBetter
//
//  Created by Gmc on 2020/3/8.
//  Copyright © 2020 Gmc. All rights reserved.
//

import UIKit

struct BBetterHolder {
    static let lessonNameHolder: String = "  请输入课程名称"
    static let bookNameHolder: String = "  请输入书籍名称"
    static let yoNameHolder: String = "  请输入小目标名称"
    static let lessonCountHolder: String = "  请输入课程总数"
    static let bookCountHolder: String = "  请输入书籍总章节数(页数)"
    static let yoCountHolder: String = "  请输入目标总量"
    static let lessonProgressHolder: String = "  当前学习进度(集/章)"
    static let bookProgressHolder: String = "  当前看书进度(章节/页)"
    static let yoProgressHolder: String = "  当前目标进度"
    static let lessonClusterHolder: String = " 这是关于哪方面的课程"
    static let bookclusterHolder: String = " 这是一本关于什么的书"
    static let yoclusterHolder: String = " 这是关于哪方面的目标"
}

enum BBetterType {
    case lesson
    case book
    case yo
}

class BBetterView: UIView {
    private var parentView:UIView?
    private var cancel:UIButton = UIButton(type: .custom)
    private var save:UIButton = UIButton(type: .custom)
    private var containerView = UIView()
    private var nameLabel = UILabel()
    private var nameInput = UITextField()
    private var countLabel = UILabel()
    private var countInput = UITextField()
    private var progressLabel = UILabel()
    private var progressInput = UITextField()
    private var clusterLabel = UILabel()
    private var clusterInput = UITextField()
    private var nameString = ""
    private var countString = ""
    private var progressString = ""
    private var clusterString = ""
    private var parameter:[String: Any] = [String: Any]()
    open var callback:(([String: Any])->Void)?
    
    init(type: BBetterType) {
        super.init(frame: .zero)
        switch type {
        case .lesson:
            do {
                nameString = BBetterHolder.lessonNameHolder
                countString = BBetterHolder.lessonCountHolder
                progressString = BBetterHolder.lessonProgressHolder
                clusterString = BBetterHolder.lessonClusterHolder
            }
        case .book:
            do {
                nameString = BBetterHolder.bookNameHolder
                countString = BBetterHolder.bookCountHolder
                progressString = BBetterHolder.bookProgressHolder
                clusterString = BBetterHolder.bookclusterHolder
            }
        case .yo:
            do {
                nameString = BBetterHolder.yoNameHolder
                countString = BBetterHolder.yoCountHolder
                progressString = BBetterHolder.yoProgressHolder
                clusterString = BBetterHolder.yoclusterHolder
            }
        }
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(gesture:)))
        addGestureRecognizer(tap)
        backgroundColor = UIColor(white: 0.3, alpha: 0.7)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func tapAction(gesture: UITapGestureRecognizer) {
        endEditing(true)
    }
    
    @objc private func cancelAction(sender: UIButton) {
        if let _ = parentView {
            hide(view: parentView!)
        }
    }
    
    @objc private func saveAction(sender: UIButton) {
        endEditing(true)
        if parameter["name"] == nil {
            return
        } else if parameter["denominator"] == nil {
            return
        } else if parameter["numerator"] == nil {
            return
        }
        let denominator = parameter["denominator"] as! Int
        let numerator = parameter["numerator"] as! Int
        if (numerator > denominator) {
            return
        }
        callback?(parameter)
        if let _ = parentView {
            hide(view: parentView!)
        }
    }
    
    open func show(view: UIView) {
        self.parentView = view
        let parentView = view
        if parentView.subviews.contains(self) {
            removeFromSuperview()
        }
        parentView.addSubview(self)
        snp.makeConstraints { (make) in
            make.edges.equalTo(parentView)
        }
        intiSubviews()
    }
    
    open func hide(view: UIView) {
        removeFromSuperview()
    }

}

extension BBetterView {
    private func intiSubviews() {
        do {
            addSubview(containerView)
            if #available(iOS 13.0, *) {
                if traitCollection.userInterfaceStyle == .dark {
                    containerView.backgroundColor = .black
                } else {
                    containerView.backgroundColor = .white
                }
            } else {
                containerView.backgroundColor = .white
            }
            containerView.layer.cornerRadius = 10
            containerView.clipsToBounds = true
            let topMargin = UIScreen.main.bounds.size.height > 813 ? 188 : 164
            containerView.snp.makeConstraints { (make) in
                make.top.equalTo(self).offset(topMargin)
                make.trailing.equalTo(self).offset(-40)
                make.leading.equalTo(self).offset(40)
                make.height.equalTo(240)
            }
        }
        do {
            cancel.setTitle("取消", for: .normal)
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                cancel.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
            } else {
                cancel.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
            }

            cancel.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
            cancel.addTarget(self, action: #selector(cancelAction(sender:)), for: .touchUpInside)
            containerView.addSubview(cancel)
            cancel.snp.makeConstraints { (make) in
                make.bottom.equalTo(containerView).offset(-6)
                make.leading.equalTo(containerView).offset(20)
            }
            save.setTitle("保存", for: .normal)
            
            if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
                save.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
            } else {
                save.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
            }

            save.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
            save.addTarget(self, action: #selector(saveAction(sender:)), for: .touchUpInside)
            containerView.addSubview(save)
            save.snp.makeConstraints { (make) in
                make.bottom.equalTo(containerView).offset(-6)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        do {
            nameLabel.text = "名称: "
            nameInput.placeholder = nameString
            nameInput.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            nameInput.layer.borderWidth = 1.0
            nameInput.layer.cornerRadius = 4.0
            nameInput.delegate = self
            nameInput.setValue(NSNumber(value: 10), forKey: "paddingLeft")
            nameInput.font = UIFont.systemFont(ofSize: 15)
            nameInput.tag = 10001
            containerView.addSubview(nameLabel)
            nameLabel.snp.makeConstraints { (make) in
                make.leading.equalTo(containerView).offset(20)
                make.top.equalTo(containerView).offset(40)
                make.width.equalTo(40)
            }
            containerView.addSubview(nameInput)
            nameInput.snp.makeConstraints { (make) in
                make.leading.equalTo(nameLabel.snp_trailing).offset(10)
                make.centerY.equalTo(nameLabel)
                make.height.equalTo(28)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        
        do {
            clusterLabel.text = "范围: "
            clusterInput.placeholder = clusterString
            clusterInput.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            clusterInput.layer.borderWidth = 1.0
            clusterInput.layer.cornerRadius = 4.0
            clusterInput.delegate = self
            clusterInput.setValue(NSNumber(value: 10), forKey: "paddingLeft")
            clusterInput.font = UIFont.systemFont(ofSize: 15)
            clusterInput.tag = 10004
            containerView.addSubview(clusterLabel)
            clusterLabel.snp.makeConstraints { (make) in
                make.leading.equalTo(nameLabel)
                make.top.equalTo(nameLabel).offset(40)
                make.width.equalTo(40)
            }
            containerView.addSubview(clusterInput)
            clusterInput.snp.makeConstraints { (make) in
                make.leading.equalTo(clusterLabel.snp_trailing).offset(10)
                make.centerY.equalTo(clusterLabel)
                make.height.equalTo(28)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        
        do {
            countLabel.text = "总数: "
            countInput.placeholder = countString
            countInput.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            countInput.delegate = self
            countInput.layer.borderWidth = 1.0
            countInput.layer.cornerRadius = 4.0
            countInput.font = UIFont.systemFont(ofSize: 15)
            countInput.keyboardType = .numberPad
            countInput.setValue(NSNumber(value: 10), forKey: "paddingLeft")
            countInput.tag = 10002
            containerView.addSubview(countLabel)
            countLabel.snp.makeConstraints { (make) in
                make.leading.equalTo(containerView).offset(20)
                make.top.equalTo(clusterLabel.snp_bottom).offset(20)
                make.width.equalTo(40)
            }
            containerView.addSubview(countInput)
            countInput.snp.makeConstraints { (make) in
                make.leading.equalTo(countLabel.snp_trailing).offset(10)
                make.centerY.equalTo(countLabel)
                make.height.equalTo(28)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
        do {
            progressLabel.text = "进度: "
            progressInput.placeholder = progressString
            progressInput.layer.borderColor = UIColor.colorWithHexString(hexString: "DDDDDF").cgColor
            progressInput.delegate = self
            progressInput.layer.borderWidth = 1.0
            progressInput.layer.cornerRadius = 4.0
            progressInput.font = UIFont.systemFont(ofSize: 15)
            progressInput.keyboardType = .numberPad
            progressInput.setValue(NSNumber(value: 10), forKey: "paddingLeft")
            progressInput.tag = 10003
            containerView.addSubview(progressLabel)
            progressLabel.snp.makeConstraints { (make) in
                make.leading.equalTo(containerView).offset(20)
                make.top.equalTo(countLabel.snp_bottom).offset(20)
                make.width.equalTo(40)
            }
            containerView.addSubview(progressInput)
            progressInput.snp.makeConstraints { (make) in
                make.leading.equalTo(progressLabel.snp_trailing).offset(10)
                make.centerY.equalTo(progressLabel)
                make.height.equalTo(28)
                make.trailing.equalTo(containerView).offset(-20)
            }
        }
    }
}

extension BBetterView: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        if let text = textField.text {
            if textField.tag == 10001 {
                parameter["name"] = text
            } else if textField.tag == 10002 {
                parameter["denominator"] = Int(text)
            } else if textField.tag == 10003 {
                parameter["numerator"] = Int(text)
            } else if textField.tag == 10004 {
                parameter["cluster"] = text
            }
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.tag == 10001 || textField.tag == 10004 {
            if (textField.markedTextRange != nil) {
                
            } else {
                if textField.text!.count > 15 {
                    return false
                }
            }
        } else if textField.tag == 10002 {
            
        } else if textField.tag == 10003 {
            
        }
        
        return true
    }
    func textFieldDidChangeSelection(_ textField: UITextField) {
        if textField.tag == 10001 || textField.tag == 10004 {
            if (textField.markedTextRange != nil) {
                
            } else {
                if textField.text!.count > 15 {
                    let string = textField.text![textField.text!.startIndex ..< textField.text!.index(textField.text!.startIndex, offsetBy: 15)]
                    textField.text! = String(string)
                } else {
                    /// toast
                }
            }
        } else if textField.tag == 10002 {
            
            
        } else if textField.tag == 10003 {
            
        }
        
    }
}
